/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.workcell.workdata.im.mvc.controller.group;

import com.workcell.workdata.im.annotation.CreateAction;
import com.workcell.workdata.im.annotation.UID;
import com.workcell.workdata.im.annotation.UpdateAction;
import com.workcell.workdata.im.entity.GroupRobot;
import com.workcell.workdata.im.model.request.AddRobotRequest;
import com.workcell.workdata.im.model.request.UpdateRobotRequest;
import com.workcell.workdata.im.model.response.ResponseEntity;
import com.workcell.workdata.im.model.vo.GroupRobotVO;
import com.workcell.workdata.im.service.GroupRobotService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@RequestMapping("/group/robot")
@Api(produces = "application/json", tags = "群机器人相关接口" )
@Validated
public class RobotController {

	@Resource
	private GroupRobotService groupRobotService;

	@ApiOperation(httpMethod = "POST", value = "创建机器人")
	@PostMapping(value = "")
	@Validated(CreateAction.class)
	public ResponseEntity<GroupRobot> add(@Valid @RequestBody AddRobotRequest request, @ApiIgnore @UID Long uid) {
		GroupRobot robot = new GroupRobot();
		robot.setUid(uid);
		robot.setGroupId(request.getGroupId());
		robot.setName(request.getName());
		robot.setWebhook(request.getWebhook());
		groupRobotService.add(robot);
		return ResponseEntity.ok(robot);
	}

	@ApiOperation(httpMethod = "POST", value = "修改机器人")
	@PostMapping(value = "/update")
	@Validated(UpdateAction.class)
	public ResponseEntity<Void> add(@Valid @RequestBody UpdateRobotRequest request) {
		GroupRobot robot = new GroupRobot();
		robot.setId(request.getId());
		robot.setName(request.getName());
		robot.setWebhook(request.getWebhook());
		groupRobotService.update(robot);
		return ResponseEntity.ok();
	}

	@ApiOperation(httpMethod = "GET", value = "获取机器人详情")
	@ApiImplicitParam(name = "id", value = "机器人ID", paramType = "path",  dataTypeClass = Integer.class,example = "0")
	@GetMapping(value = "/{id}")
	public ResponseEntity<GroupRobotVO> get(@PathVariable long id) {
		return ResponseEntity.ok(groupRobotService.findOne(id));
	}

	@ApiOperation(httpMethod = "DELETE", value = "删除机器人")
	@ApiImplicitParam(name = "id", value = "机器人ID", paramType = "path",  dataTypeClass = Integer.class,example = "0")
	@DeleteMapping(value = "/{id}")
	public ResponseEntity<Void> delete(@PathVariable long id, @ApiIgnore @UID Long uid) {
		return groupRobotService.delete(id,uid);
	}


	@ApiOperation(httpMethod = "POST", value = "关闭机器人")
	@ApiImplicitParam(name = "id", value = "机器人ID", paramType = "path",  dataTypeClass = Integer.class,example = "0")
	@PostMapping(value = "/disable/{id}")
	public ResponseEntity<Void> disable(@PathVariable long id, @ApiIgnore @UID Long uid) {

		groupRobotService.disable(id,uid);

		return ResponseEntity.ok();
	}

	@ApiOperation(httpMethod = "POST", value = "启用机器人")
	@ApiImplicitParam(name = "id", value = "机器人ID", paramType = "path",  dataTypeClass = Integer.class,example = "0")
	@PostMapping(value = "/enable/{id}")
	public ResponseEntity<Void> enable(@PathVariable long id, @ApiIgnore @UID Long uid) {

		groupRobotService.enable(id,uid);

		return ResponseEntity.ok();
	}

}
